﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Diagnostics.Contracts;

namespace YasharEl.Infrastructure.ObjectFactory.Unity
{
    public class UnityDependencyRegistrationTraverser : IDependencyRegistrationTraverser
    {
        private readonly IUnityContainer unityContainer;

        public UnityDependencyRegistrationTraverser(IUnityContainer unityContainer)
        {
            Contract.Requires<ArgumentNullException>(unityContainer != null, "unityContainer could not be null.");

            this.unityContainer = unityContainer;
        }

        public void Traverse(Action<DependencyRegistration> traverseAction)
        {
            foreach (ContainerRegistration containerRegistration in unityContainer.Registrations)
            {
                traverseAction(
                    new DependencyRegistration(
                        containerRegistration.MappedToType,
                        containerRegistration.RegisteredType,
                        containerRegistration.LifetimeManagerType.FullNameToString(),
                        containerRegistration.Name
                    )
                );
            }
        }
    }
}
